package cn.iocoder.yudao.framework.license;

import cn.iocoder.yudao.framework.common.exception.KnownServiceException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  使用过滤器方式实现许可证校验
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author plliang
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/04/05
 * @link <a href="https://gitee.com/plliang/springboot-license/blob/master/springboot-license-starter/springboot-license-starter/src/main/java/com/plliang/license/filter/LicenseCheckFilter.java">LicenseCheckFilter.java</a>
 */
public class LicenseCheckFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            LicenseHandler.checkLicense();
        } catch (KnownServiceException e) {
            ServletUtils.writeJSON(response, CommonResult.error(e.getCode(), e.getMessage()));
            return;
        }
        chain.doFilter(request, response);
    }

}